package com.qk.management.controller;

import com.qk.common.Result;
import com.qk.common.util.AliYunOSSOperators;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;
import java.util.UUID;


/**
 * @文件名: FileHandlcController
 * @作者: DX
 * @创建日期: 2025/10/8
 * @描述:
 */
@Slf4j
@RestController
public class FileHandlcController {

    @Autowired
    private AliYunOSSOperators oss;

    @PostMapping("/upload")
    public Result upload(MultipartFile image) {
        log.info("上传文件:{}", image);

        try {
            Objects.requireNonNull( image, "上传文件不能为空");
            Objects.requireNonNull( image.getOriginalFilename(), "上传文件名不能为空");
            return Result.success(oss.upload(image.getBytes(), UUID.randomUUID() + image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf(".")) ));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
